package com.jzo2o.customer.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.expcetions.BadRequestException;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.common.utils.StringUtils;
import com.jzo2o.customer.mapper.AgencyCertificationAuditMapper;
import com.jzo2o.customer.model.domain.AgencyCertificationAudit;
import com.jzo2o.customer.model.domain.AgencyCertificationAuditDTO;
import com.jzo2o.customer.model.dto.request.AgencyCertificationAuditPageQueryReqDTO;
import com.jzo2o.customer.service.IAgencyCertificationAuditService;
import com.jzo2o.mvc.utils.UserContext;
import com.jzo2o.mysql.utils.PageHelperUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * <p>
 * 机构认证审核记录表 服务实现类
 * </p>
 *
 * @author author
 * @since 2025-07-01
 */
@Service
public class AgencyCertificationAuditServiceImpl extends ServiceImpl<AgencyCertificationAuditMapper, AgencyCertificationAudit> implements IAgencyCertificationAuditService {
    /**
     * 分页查询机构认证审核记录
     *
     * @param dto
     * @return
     */
    @Override
    public PageResult<AgencyCertificationAuditDTO> queryPage(AgencyCertificationAuditPageQueryReqDTO dto) {
        PageResult<AgencyCertificationAuditDTO> workerCertificationAuditPageResult = PageHelperUtils.selectPage(dto, () -> getBaseMapper().queryPage(dto));
        return workerCertificationAuditPageResult;
    }


    /**
     * 审核机构认证信息
     *
     * @param id
     * @param certificationStatus
     * @param rejectReason
     */
    @Override
    public void waarmerk(Long id, Integer certificationStatus, String rejectReason) {
        //健壮性判断
        if (id == null) {
            throw new BadRequestException("请选择要审核的认证信息");
        }
        if (certificationStatus == null) {
            throw new BadRequestException("请选择认证状态");
        }
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String format = dateTimeFormatter.format(LocalDateTime.now());

        //更新
        boolean update = lambdaUpdate()
                .eq(AgencyCertificationAudit::getId, id)
                .set(AgencyCertificationAudit::getCertificationStatus, certificationStatus)
                .set(rejectReason != null && StringUtils.isNotBlank(rejectReason), AgencyCertificationAudit::getRejectReason, rejectReason)
                .set(AgencyCertificationAudit::getAuditTime, format)
                .set(AgencyCertificationAudit::getAuditStatus, 1)
                .set(AgencyCertificationAudit::getAuditorId, UserContext.currentUserId())
                .set(AgencyCertificationAudit::getAuditorName, UserContext.currentUser().getName())
                .update();
        if (!update) {
            throw new BadRequestException("认证信息审核失败");
        }
    }
}
